package com.example.smartcitydemo.ui.job.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.activityViewModels
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.DeliverHistoryBean
import com.example.smartcitydemo.databinding.FragmentJobSecondBinding
import com.example.smartcitydemo.ext.init
import com.example.smartcitydemo.ui.job.DeliverHistoryAdapter
import com.example.smartcitydemo.viewModel.job.WorkViewModel
import com.example.smartcitydemo.viewModel.person.PersonViewModel

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.job.fragment
 * @NAME: Caleb.F
 * @DATE: 2021/11/18 9:34
 * @Email: 1919796283@qq.com
 */
class JobSecondFragment : BaseFragment<FragmentJobSecondBinding>(R.layout.fragment_job_second) {
    private val workViewModel: WorkViewModel by activityViewModels()
    private val personViewModel: PersonViewModel by activityViewModels()


    private val deliverAdapter: DeliverHistoryAdapter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        DeliverHistoryAdapter().apply {
            init()
            setDiffCallback(DeliverHistoryBean.diffCallBack)
        }
    }


    override fun initView(myViewBinding: FragmentJobSecondBinding, savedInstanceState: Bundle?) {
        myViewBinding.apply {
            toolbarWorkSecond.tvContent.text = "投递历史记录"
            ryWorkSecond.apply {
                adapter = deliverAdapter
            }
        }
    }


    override fun initData() {
        personViewModel.getPersonInfo()
    }

    override fun addObservers(bindView: FragmentJobSecondBinding) {
        workViewModel.deliverHistoryLiveData.observe(this) {
            when (it) {
                is Resource.Success -> deliverAdapter.setDiffNewData(it.data!!.toMutableList())
                is Resource.Failure -> deliverAdapter.setEmptyView(R.layout.error_layout)
                is Resource.Loading -> deliverAdapter.setEmptyView(R.layout.loading_layout)
            }
        }
        personViewModel.personInfo.observe(this) {
            if (it is Resource.Success) {
                workViewModel.getDiliverHistory(it.data?.userId ?: 0)
            }
        }
    }
}